package com.ygswglxt.leave.controller;

import com.ygswglxt.leave.entity.LeaveBalance;
import com.ygswglxt.leave.service.LeaveBalanceService;
import com.ygswglxt.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/leave/balance")
public class LeaveBalanceController {
    
    @Autowired
    private LeaveBalanceService leaveBalanceService;
    
    /**
     * 获取所有员工的假期余额（HR使用）
     */
    @GetMapping("/all")
    public Result<List<LeaveBalance>> getAllBalances(HttpServletRequest request) {
        String role = request.getHeader("role");
        if (!"HR".equals(role)) {
            return Result.error(403, "无权限访问，仅HR角色可以查看所有员工的假期余额");
        }
        return leaveBalanceService.getAllBalances();
    }
    
    /**
     * 获取当前用户的假期余额
     */
    @GetMapping("/my")
    public Result<LeaveBalance> getMyBalance(HttpServletRequest request) {
        Long userId = Long.parseLong(request.getHeader("userId"));
        return leaveBalanceService.getBalanceByUserId(userId);
    }
    
    /**
     * 获取指定员工的假期余额（HR/Manager使用）
     */
    @GetMapping("/{userId}")
    public Result<LeaveBalance> getBalanceByUserId(@PathVariable Long userId, HttpServletRequest request) {
        String role = request.getHeader("role");
        if (!"HR".equals(role) && !"MANAGER".equals(role)) {
            return Result.error(403, "无权限访问");
        }
        return leaveBalanceService.getBalanceByUserId(userId);
    }
    
    /**
     * 更新员工的假期余额（HR使用）
     */
    @PostMapping("/update")
    public Result<String> updateBalance(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String role = request.getHeader("role");
        if (!"HR".equals(role)) {
            return Result.error(403, "无权限访问，仅HR角色可以更新假期余额");
        }
        
        try {
            Long userId = Long.parseLong(params.get("userId").toString());
            BigDecimal annualBalance = params.get("annualBalance") != null ? 
                new BigDecimal(params.get("annualBalance").toString()) : null;
            BigDecimal sickBalance = params.get("sickBalance") != null ? 
                new BigDecimal(params.get("sickBalance").toString()) : null;
            BigDecimal personalBalance = params.get("personalBalance") != null ? 
                new BigDecimal(params.get("personalBalance").toString()) : null;
            BigDecimal adjustmentBalance = params.get("adjustmentBalance") != null ? 
                new BigDecimal(params.get("adjustmentBalance").toString()) : null;
            String reason = params.get("reason") != null ? params.get("reason").toString() : "手动更新";
            Long operatorId = Long.parseLong(request.getHeader("userId"));
            
            return leaveBalanceService.updateBalance(
                userId, annualBalance, sickBalance, personalBalance, adjustmentBalance, reason, operatorId
            );
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 增加员工的假期余额（HR使用）
     */
    @PostMapping("/increase")
    public Result<String> increaseBalance(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String role = request.getHeader("role");
        if (!"HR".equals(role)) {
            return Result.error(403, "无权限访问，仅HR角色可以增加假期余额");
        }
        
        try {
            Long userId = Long.parseLong(params.get("userId").toString());
            String leaveType = params.get("leaveType").toString();
            BigDecimal days = new BigDecimal(params.get("days").toString());
            String reason = params.get("reason") != null ? params.get("reason").toString() : "手动增加";
            Long operatorId = Long.parseLong(request.getHeader("userId"));
            
            return leaveBalanceService.increaseBalance(userId, leaveType, days, reason, operatorId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("增加失败: " + e.getMessage());
        }
    }
}

